package com.jwsoft.manager.core.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IKAnalyzerUtil {
    private static final Pattern PATTERN_TWON_NUMBER = Pattern.compile("[\\s\\S]+(乡|路|镇|街道|乡镇|街|道|巷)([\\d-]+号)?");
    private static final Pattern PATTERN_VILLAGE = Pattern.compile("[\\s\\S]+(村|墅|院|楼|邸|社区|小区|公园|花园|庭|阁|居|轩|苑|庄|巷|园|坊|湾|城|馆|区|门|郡)");

    private static final Pattern PATTERN_HOUSE_UNIT_ROOM_NUMBER =
            Pattern.compile("([a-zA-Z\\d-]+[幢号栋])?([零一-九\\d]+单元)?([a-zA-Z\\d-]+室[、]?)*$");


    /**
     * 地址分词，返回string 分词结果 使用 | 隔开
     * @param msg
     * @return
     * @throws IOException
     */
    public static String wordSegmentationToString(String msg) throws IOException {
        msg = removeSpecificCharacters(msg);
        StringReader sr=new StringReader(msg);
        IKSegmenter ik=new IKSegmenter(sr, true);
        Lexeme lex=null;
        StringBuilder sb = new StringBuilder();
        while (((lex=ik.next()) != null)) {
            sb.append(lex.getLexemeText()).append("|");
        }
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        }else{
            return "";
        }
    }

    /**
     * 地址分词，返回分词数组
     * @param msg
     * @return
     * @throws IOException
     */
    public static List<String> wordSegmentationToList(String msg) throws IOException {
        msg = removeSpecificCharacters(msg);
        StringReader sr=new StringReader(msg);
        IKSegmenter ik=new IKSegmenter(sr, true);
        Lexeme lex=null;
        List<String> list = new ArrayList<>();
        while((lex=ik.next())!=null){
            list.add(lex.getLexemeText());
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
//        ArrayList<String> addressList = new ArrayList<>();
//        addressList.add("浙江省金华市婺城区塔石乡银岭村张村自然村和谐路３５号");
//
//        for (String text : addressList) {
//            List<String> strings = IKAnalyzerUtil.wordSegmentationToList(text);
//            System.out.println(strings);
//
//        }
//
//        String address = "金东金港大道西669号豪都·金尊华府124幢2-3205室";

//        Matcher matcher = PATTERN_TWON_NUMBER.matcher(address);
//        matcher.region(0, address.indexOf("号") + 1);
//
//        // 使用 find 方法查找匹配
//        if (matcher.find()) {
//            // 返回匹配的部分
//            String matcherText = matcher.group();
//            System.out.println(matcherText);
//        } else {
//            System.out.println("未找到匹配的部分");
//        }

//        Matcher matcher = PATTERN_VILLAGE.matcher(address);
//        String matchedPart = "";
//        if (matcher.find()) {
//            // 获取匹配的部分
//            matchedPart = matcher.group();
//            // 输出匹配的部分
//            System.out.println("匹配的部分: " + matchedPart);
//        }
//        System.out.println(address.replace(matchedPart,""));
        String inputString = "竟达·风渡(竟达·星河湾三期)38幢1-501室";

        // 创建匹配器
        Pattern pattern = Pattern.compile("([\\s\\S]+)(村|墅|院|楼|府|邸|社区|期|小区|公园|花园|庭|阁|居|轩|苑|庄|巷|园|坊|湾|城|馆|区|门|郡)?/\\(/");



        Matcher matcher = pattern.matcher(inputString);

        // 使用 find 方法查找匹配
        if (matcher.find()) {
            // 输出匹配的信息
            String fullString = matcher.group();
             System.out.println(fullString);
         } else {
            System.out.println("未找到匹配的信息");
        }
    }

    private static String removeSpecificCharacters(String input) {
        // 使用正则表达式将特定字符替换为空字符串
        return input.replaceAll("[·\\-！#]", "");
    }
}
